const Koa = require('koa');
const app = new Koa();
var db = require("./database/config/db");
const bcrypt = require('bcrypt');
const saltRounds = 10;

const bodyParser = require('koa-bodyparser');
app.use(bodyParser());

const cors = require('koa2-cors');
app.use(cors());

const Router = require('koa-router');
const user = require('./appAPI/user');
const home = require('./appAPI/home');
const goods = require('./appAPI/goods');
const search = require('./appAPI/search');
const comments = require('./appAPI/comments');

// 装载所有子路由
const router = new Router();
router.use('/user', user.routes());
router.use('/home', home.routes());
router.use('/goods', goods.routes());
router.use('/search', search.routes());
router.use('/comments', comments.routes());


// 加载路由中间件
app.use(router.routes());
app.use(router.allowedMethods()); // 只允许我们自己设置的methods（POST,GET,DELETE,PUT）方法请求接口。

app.use(async ctx => {
    ctx.body = "我是首页";
});

// app.use(async ctx => {
//     // 加盐
//     var salt = bcrypt.genSaltSync(saltRounds);
//     // 生成hash密码
//     var hash = bcrypt.hashSync('233', salt);

//     await db.UPDATE_DATA('test', {
//         pwd: hash
//     }, {name: 'Jerry'}).then(data => {
//         ctx.body = JSON.stringify(data);
//     }, err => {
//         console.log(err);
//     });
// });

app.listen(1213, () => {
    console.log("[Server] starting at port 1213");
});